<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';

if($_RQ['op']=='get'){
	$return['item']=array();
	$htmls=array(
			'type'=>'edit',
			'post'=>'post',
			'desc'=>'下载对账单',
			'edit'=>array(
					array('type'=>'radio','title'=>'账单类型','name'=>'type','options'=>'types' ),
					array('type'=>'radio','title'=>'数据类型','name'=>'datatype','options'=>'datatypes' ),
					array('type'=>'time_range','title'=>'账单时间','name'=>'time' ),
				)
				);
		$return['types']=array('1'=>'所有账单','2'=>'支付账单','3'=>'退款帐单','4'=>'撤销账单');
		$return['datatypes']=array('1'=>'所有账单','2'=>'仅商城账单');
		$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$url=download_url('jy_weishop/finance.downloadbill/export/type='.$post['type'].'&datatype='.$post['datatype']);
	exi('','',$url);
}
if($_RQ['op']=='export'){
	
	$types=array('1'=>'所有账单','2'=>'支付账单','3'=>'退款帐单','4'=>'撤销账单');
	$datatypes=array('1'=>'所有账单','2'=>'仅商城账单');
	$typestitle = $types[$_RQ['type']];
	$datatypestitle = $datatypes[$_RQ['datatype']];
	$params=array();
	if($_RQ['type'] ==2){
		$params['ispay']=1;
	}
	if($_RQ['type'] ==3){
		$params['status']=6;
	}
	if($_RQ['type'] ==4){
		$params['status']=5;
	}
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭','5'=>'申请维权','6'=>'已维权');
	$buytype=array('0'=>'普通','1'=>'多商户','2'=>'货到付款','3'=>'自提','4'=>'加星','5'=>'砍价','6'=>'拼团','7'=>'秒杀','8'=>'抽奖','9'=>'夺宝');
	$list=pdo_getall('jy_weishop_order',$params,array(),'','createtime asc');
	if($list){
		foreach($list as &$v){
			$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');
			if(!$v['nickname']){
				$v['nickname']='匿名用户';
			}
			$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
			$v['status'] = $status[$v['status']];
			$v['buytype'] = $buytype[$v['buytype']];
			$v['ispay'] = $v['ispay'] ==1 ? '已支付' : '未支付';
			$v['paytype'] = get_pay_types()[$v['paytype']];
		}
	}
	$header=array(
		'id'=>'订单ID',
		'ordersn'=>'订单号',
		'mid'=>'买家ID',
		'nickname'=>'买家',
		'price'=>'金额',
		'status'=>'订单状态',
		'buytype'=>'订单类型',
		'createtime'=>'下单时间',
		'ispay'=>'是否支付',
		'paytype'=>'支付方式',
	);
	cfc('tools')->export2excel($header,$list,$typestitle.$datatypestitle.'-对账单'.date('Y/m/d',time()));
	
}